package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.UmsCustomer;
import com.ruoyi.system.domain.params.CounterGoodsParams;
import com.ruoyi.system.domain.params.SaveCounterGoodsGoodsParams;
import com.ruoyi.system.domain.params.SaveOrderParams;
import com.ruoyi.system.domain.vo.CounterGoodsListVo;
import com.ruoyi.system.domain.vo.EditCounterGoodsInfoVo;
import com.ruoyi.system.service.IUmsCustomerService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.WhCounterGoodsMapper;
import com.ruoyi.system.domain.WhCounterGoods;
import com.ruoyi.system.service.IWhCounterGoodsService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 货柜货物Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-07
 */
@Service
public class WhCounterGoodsServiceImpl implements IWhCounterGoodsService {
    @Resource
    private WhCounterGoodsMapper whCounterGoodsMapper;
    @Resource
    private IUmsCustomerService customerService;

    /**
     * 查询货柜货物
     *
     * @param id 货柜货物主键
     * @return 货柜货物
     */
    @Override
    public WhCounterGoods selectWhCounterGoodsById(Integer id) {
        return whCounterGoodsMapper.selectWhCounterGoodsById(id);
    }

    /**
     * 根据id查询编辑详情
     * @param id 货物id
     * @return
     */
    @Override
    public EditCounterGoodsInfoVo selectCounterGoodsEditInfo(Integer id) {
        return whCounterGoodsMapper.selectCounterGoodsEditInfo(id);
    }

    /**
     * 查询货柜货物列表
     *
     * @param whCounterGoods 货柜货物
     * @return 货柜货物
     */
    @Override
    public List<WhCounterGoods> selectWhCounterGoodsList(WhCounterGoods whCounterGoods) {
        return whCounterGoodsMapper.selectWhCounterGoodsList(whCounterGoods);
    }

    /**
     * 根据货柜id查询货柜货物列表（带客户信息）
     * @param params
     * @return
     */
    @Override
    public List<CounterGoodsListVo> findWhCounterGoodsList(CounterGoodsParams params) {
        if (params.getCounterId() == null) {
            throw new ServiceException("非法参数");
        }
        List<CounterGoodsListVo> list = whCounterGoodsMapper.findWhCounterGoodsList(params);
        return list == null ? new ArrayList<>() : list;
    }

    /**
     * 新增货柜货物
     *
     * @param whCounterGoods 货柜货物
     * @return 结果
     */
    @Override
    public int insertWhCounterGoods(WhCounterGoods whCounterGoods) {
        whCounterGoods.setCreateTime(DateUtils.getNowDate());
        return whCounterGoodsMapper.insertWhCounterGoods(whCounterGoods);
    }

    /**
     * 获取用户id
     * @param params
     * @return
     */
    public Integer findUserId(SaveCounterGoodsGoodsParams params) {
        SaveOrderParams saveUser = new SaveOrderParams();
        saveUser.setCustomerName(params.getCustomerName());
        saveUser.setCustomerPhone(params.getCustomerPhone());
        saveUser.setCreateBy("s_" + params.getCreateBy());
        saveUser.setCreateTime(new Date());
        UmsCustomer orderCustomerInfo = customerService.createOrderCustomerInfo(saveUser);
        if (orderCustomerInfo == null) {
            throw new ServiceException("创建新客户失败");
        }
        return orderCustomerInfo.getUserId();
    }

    /**
     * 添加货柜货物
     * @param params
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveWhCounterGoods(SaveCounterGoodsGoodsParams params) {
        boolean verifyParams = params.getCounterId() == null
                || StringUtils.isEmpty(params.getCustomerGoods())
                || StringUtils.isEmpty(params.getCustomerName());
        if (verifyParams) {
            throw new ServiceException("非法参数");
        }
        // 添加新客户
        if (params.getCustomerId() == null) {
            params.setCustomerId(findUserId(params));
        }
        WhCounterGoods goods = new WhCounterGoods();
        BeanUtils.copyProperties(params,goods);
        goods.setIsDelete(1);
        return whCounterGoodsMapper.insertWhCounterGoods(goods);
    }

    /**
     * 修改货柜货物
     * @param params 货柜货物
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateWhCounterGoods(SaveCounterGoodsGoodsParams params) {
        boolean verifyParams = params.getId() == null
                || params.getCounterId() == null
                || (params.getCustomerId() == null && StringUtils.isEmpty(params.getCustomerName()));
        if (verifyParams) {
            throw new ServiceException("非法参数");
        }
        // 添加新客户
        if (params.getCustomerId() == null) {
            params.setCustomerId(findUserId(params));
        }
        WhCounterGoods goods = new WhCounterGoods();
        BeanUtils.copyProperties(params,goods);
        return whCounterGoodsMapper.updateWhCounterGoods(goods);
    }

    /**
     * 批量删除货柜货物
     *
     * @param ids 需要删除的货柜货物主键
     * @return 结果
     */
    @Override
    public int deleteWhCounterGoodsByIds(Integer[] ids) {
        return whCounterGoodsMapper.deleteWhCounterGoodsByIds(ids);
    }

    /**
     * 删除货柜货物信息
     *
     * @param id 货柜货物主键
     * @return 结果
     */
    @Override
    public int deleteWhCounterGoodsById(Integer id) {
        return whCounterGoodsMapper.deleteWhCounterGoodsById(id);
    }
}
